package com.tech.tool.controller;

import com.tech.tool.constant.CookieConstant;
import com.tech.tool.constant.RedisConstant;
import com.tech.tool.dataobject.UserInfo;
import com.tech.tool.dto.userInfo.LoginDTO;
import com.tech.tool.dto.userInfo.SelectUserListDTO;
import com.tech.tool.dto.userInfo.UserInfoDTO;
import com.tech.tool.service.UserInfoService;
import com.tech.tool.utils.CookieUtil;
import com.tech.tool.utils.ListVOUtil;
import com.tech.tool.utils.ResultVOUtil;
import com.tech.tool.utils.SaveVOUtil;
import com.tech.tool.vo.ListVO;
import com.tech.tool.vo.ResultVO;
import com.tech.tool.vo.SaveVO;
import com.tech.tool.vo.userInfo.SelectUserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @PostMapping(path = "/login",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO login(@RequestBody LoginDTO loginDTO, HttpServletResponse response) {

        //1. 判断用户信息，待补充
        UserInfo userInfo = userInfoService.findByUserLogin(loginDTO.getLoginName());
        if (loginDTO.getPassword().equals(userInfo.getUserPassword())) {
            String code = userInfo.getUserEmail();

            //2. 设置token到redis
            String token = UUID.randomUUID().toString();
            Integer expire = RedisConstant.EXPIRE;

            //3. 设置token到cookie
            redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), code, expire, TimeUnit.SECONDS);
            CookieUtil.set(response, CookieConstant.TOKEN, token, expire);

            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(0,"密码错误");
        }


    }

    @PostMapping(path = "/logout",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO login(HttpServletRequest request, HttpServletResponse response) {

        //1. 从cookie里查询
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);

        //2. 清除redis
        redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));

        //3. 清除cookie
        CookieUtil.set(response, CookieConstant.TOKEN, null, 0);

        return ResultVOUtil.success();

    }

    @PostMapping(path = "/selectUserList",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO selectUserList(@RequestBody SelectUserListDTO selectUserListDTO) {
        Page<UserInfo> userInfoPage = userInfoService.findByUserName(selectUserListDTO.getUserName());
        List<SelectUserListVO> selectUserListVOList = new ArrayList<>();

        for (UserInfo userInfo: userInfoPage.getContent()) {
            SelectUserListVO selectUserListVO = new SelectUserListVO();
            BeanUtils.copyProperties(userInfo, selectUserListVO);
            selectUserListVOList.add(selectUserListVO);
        }

        ListVO listVO = new ListVOUtil().setPageInfo(userInfoPage, selectUserListVOList);
        listVO.setList(selectUserListVOList);

        return ResultVOUtil.success(listVO);

    }

    @PostMapping(path = "/save",
            consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResultVO save(@RequestBody UserInfoDTO userInfoDTO) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoDTO, userInfo);
        UserInfo result = userInfoService.save(userInfo);
        SaveVO saveVO = SaveVOUtil.setId(result.getId());
        return ResultVOUtil.success(saveVO);
    }
}
